/*
 * @Description 
 * @Autor lzhiqiang
 * @Date 2023-04-05 13:50:19
 * @LastEditors lzhiqiang
 * @LastEditTime 2023-04-05 14:10:12
 */
#include "ws_servlet.h"

namespace johnsonli {
namespace http {

    FunctionWSServlet::FunctionWSServlet(callback cb
                                        ,on_connect_cb connect_cb
                                        ,on_close_cb close_cb)
        :WSServlet("FunctionWSServlet")
        ,m_callback(cb)
        ,m_onConnect(connect_cb)
        ,m_onClose(close_cb) {
    }

    int32_t FunctionWSServlet::onConnect(johnsonli::http::HttpRequest::ptr header
                                        ,johnsonli::http::WSSession::ptr session) {
        if(m_onConnect) {
            return m_onConnect(header, session);
        }
        return 0;
    }

    int32_t FunctionWSServlet::onClose(johnsonli::http::HttpRequest::ptr header
                                        ,johnsonli::http::WSSession::ptr session) {
        if(m_onClose) {
            return m_onClose(header, session);
        }
        return 0;
    }

    int32_t FunctionWSServlet::handle(johnsonli::http::HttpRequest::ptr header
                                    ,johnsonli::http::WSFrameMessage::ptr msg
                                    ,johnsonli::http::WSSession::ptr session) {
        if(m_callback) {
            return m_callback(header, msg, session);
        }
        return 0;
    }

    WSServletDispatch::WSServletDispatch(const std::string& servername)
    : ServletDispatch(servername){
        m_name = "WSServletDispatch";
    }

    void WSServletDispatch::addServlet(const std::string& uri
                        ,FunctionWSServlet::callback cb
                        ,FunctionWSServlet::on_connect_cb connect_cb
                        ,FunctionWSServlet::on_close_cb close_cb) {
        ServletDispatch::addServlet(uri, std::make_shared<FunctionWSServlet>(cb, connect_cb, close_cb));
    }

    void WSServletDispatch::addGlobServlet(const std::string& uri
                        ,FunctionWSServlet::callback cb
                        ,FunctionWSServlet::on_connect_cb connect_cb
                        ,FunctionWSServlet::on_close_cb close_cb) {
        ServletDispatch::addGlobServlet(uri, std::make_shared<FunctionWSServlet>(cb, connect_cb, close_cb));
    }

    WSServlet::ptr WSServletDispatch::getWSServlet(const std::string& uri) {
        auto slt = getMatchedServlet(uri);
        return std::dynamic_pointer_cast<WSServlet>(slt);
    }

}
}
